package org.example.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ExecutorPoolDemo {

    public   int count = 1;

    public static void main(String[] args) {
        ExecutorPoolDemo executorPoolDemo01 = new ExecutorPoolDemo();
        ExecutorPoolDemo executorPoolDemo02 = new ExecutorPoolDemo();
        ExecutorService executorService = new ThreadPoolExecutor(8,16,30, TimeUnit.SECONDS
        ,new LinkedBlockingQueue<>(50),
                new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略


        executorService.execute(()->{
            executorPoolDemo01.count++;
            System.out.println(Thread.currentThread().getName()+":"+executorPoolDemo01.count);
        });


        executorService.execute(()->{
            try {
                TimeUnit.MILLISECONDS.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName()+":"+executorPoolDemo02.count);
        });

//        for(int i = 0 ; i < 1000; i++){
//            //无返回值
//            executorService.execute(()->{
//                try {
//                    TimeUnit.MILLISECONDS.sleep(50);
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//                System.out.println(Thread.currentThread().getName());
//            });
//        }
//
//        executorService.execute(()->{
//            try {
//                TimeUnit.MILLISECONDS.sleep(500);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            System.out.println(Thread.currentThread().getName()+"::::end");
//        });
        executorService.shutdown();
    }
}
